package com.gxa.community.admin.controller.agent;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gxa.community.admin.service.agent.AgentService;
import com.gxa.community.admin.service.agent.UserAgentApplicationService;
import com.gxa.community.admin.service.order.OrderService;
import com.gxa.community.admin.service.order.WayBillService;
import com.gxa.community.core.common.RegExpression;
import com.gxa.community.core.pojo.bo.agent.AgentAppNoAndReason;
import com.gxa.community.core.pojo.dto.customer.dto.UserAgentApplicationDto;
import com.gxa.community.core.pojo.vo.agent.AgentVo;
import com.gxa.community.core.pojo.vo.order.OrderInfoVo;
import com.gxa.community.core.pojo.vo.order.WayBillVo;
import com.gxa.community.core.utils.LittleUtils;
import com.gxa.community.core.utils.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

@Validated
@RestController
@RequestMapping("/agent")
public class AgentController {
    // TODO 数据校验

    final UserAgentApplicationService userApplicationAgent;
    final AgentService agentService;
    final OrderService orderService;
    final WayBillService wayBillService;


    public AgentController(AgentService agentService, UserAgentApplicationService userApplicationAgent, OrderService orderService, WayBillService wayBillService) {
        this.agentService = agentService;
        this.userApplicationAgent = userApplicationAgent;
        this.orderService = orderService;
        this.wayBillService = wayBillService;
    }

    /**
     * 查询团长申请信息
     *
     * @param a 查询条件
     */
    @PostMapping("/queryApplication")
    public R queryApplication(Integer page, Integer size, @RequestBody AgentVo a) {
        // 数据校验
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1 || size > 100) size = 5;

        if (LittleUtils.checkNum(a.getPointType(), 1, 6)) return R.error("自提点类型有误");
        if (LittleUtils.checkNum(a.getStatus(), -1, 1)) return R.error("审核状态有误");

        if (StringUtils.isNotBlank(a.getPhone()))
            if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), a.getPhone()))
                return R.error("手机号有误");

        return agentService.queryApplication(page, size, a);
    }

    /**
     * 查询团长信息
     *
     * @param a 查询条件
     */
    @PostMapping("/queryAgent")
    public R queryAgent(Integer page, Integer size, @RequestBody AgentVo a) {
        return queryApplication(page, size, a);
    }

    /**
     * 同意团长申请/修改团长信息为有效
     *
     * @param nos 团长申请编号
     */
    @PutMapping("/agreeApplication")
    public R agreeApplication(@RequestBody List<Integer> nos) {
        return agentService.agreeApplication(nos);
    }

    /**
     * 拒绝团长申请/修改团长信息为无效
     *
     * @param nos 团长申请编号及拒绝理由
     */
    @PutMapping("/rejectApplication")
    public R rejectApplication(@RequestBody AgentAppNoAndReason nos) {
        if (StringUtils.isBlank(nos.getReason())) return R.error("拒绝理由不能为空");
        return agentService.rejectApplication(nos);
    }

    /**
     * 团长申请
     *
     * @param userAgentApplicationDto
     * @return
     */
    @PostMapping("/agent/application")
    public R addUserApplicationAgent(@RequestBody UserAgentApplicationDto userAgentApplicationDto) {
        return userApplicationAgent.addUserApplicationAgent(userAgentApplicationDto);
    }
    /**
     * 查询团长属下的订单
     */
    @GetMapping("/queryOrder")
    public R queryOrder(Integer page, Integer size, Integer status) {

        // TODO 获取团长ID
        Integer agentId = 10001;

        //1未发货 2待提货 3已提货
        if (LittleUtils.checkNum(status, 1, 3)) return R.error("订单状态有误");
        if (status == 1 || status == 2) status += 10; // 特殊处理

        return orderService.queryOrder(page, size, new OrderInfoVo().setAgentId(agentId).setStatus(status));
    }

    /**
     * 查询团长属下的运单
     */
    @GetMapping("/queryWayBill")
    public R queryWayBill(Integer page, Integer size, Integer status) {

        // TODO 获取团长ID
        Integer agentId = 10001;

        //1待配送 2配送中 3已送达 4已确认收货
        if (LittleUtils.checkNum(status, 1, 4)) return R.error("运单状态有误");

        return wayBillService.queryWaybill(page, size, new WayBillVo().setAgentId(agentId).setStatus(status));
    }

    /**
     * 查询团长属下的订单的商品信息
     *
     * @param orderNo 待查询的订单号列表
     */
    @PostMapping("/queryGoods")
    public R queryGoods(@RequestBody List<String> orderNo) {
        // TODO 获取团长ID
        Integer agentId = 10001;

        return agentService.queryGoods(agentId, orderNo);
    }


    @PostMapping("/index/infos")
    public R getAllInfos(Integer userId){
        R r = agentService.queryAgentIndex(userId);
        return r;
    }

    @PostMapping("index/wellgoods")
    public R getWellGoods(){
        R r = agentService.wellGoodsShare();
        return r;
    }

    @PostMapping("index/seckillgoods")
    public R getTodaySecKillGoods(){
        R r = agentService.todaySeckill();
        return r;
    }

    @PostMapping("index/groupgoods")
    public R getTodayGroupGoods(){
        R r = agentService.todaySeckill();
        return r;
    }
}
